--[[
   Copyright (c) 2012 TPV R&D Center Brazil
   All rights reserved
 
   FileName: app.input
 
   Created by: Guilherme Guimarães
   Date: 10/09/2012
--]]
------------------------------------------------------------------------
--requires
------------------------------------------------------------------------
--local variables
local tSourceList = {}
local tCurSource = {}
------------------------------------------------------------------------
--module table
--Input module, responsible of sources management and integration with Focus Manager
local tInputApp = {}
------------------------------------------------------------------------
--module functions

--- Get the current source
-- @return the actual source
function tInputApp.getCurrentSource()
   local tSource = control.getCurrentSource()
   tCurSource['nId'] = tSource['id']
   if (tSource['type'] == 'Tuner') then
      tCurSource['sName'] = 'TV'
   else
      tCurSource['sName'] = tSource['name']
   end
   tCurSource['bConnected'] = tSource['connected']
   tCurSource['sScreenName'] = tSource['screenName']
   tCurSource['sType'] = tSource['type']
   return tCurSource
end

--- Set the current source
-- @param tSource the actual source
-- @return boolean True if the source is plugged, false otherwise.
function tInputApp.setCurrentSource(tSource)
   control.setCaption()
   control.selectSource(tSource.nId)
end

--- Get the list of sources
-- @return table with all sources
function tInputApp.getSourceList()
   return tSourceList
end

--- Function to update the source list
-- @return A table of sources {nId, sName, bConnected, sScreenName and sType}
function tInputApp.updateSourceList()
   tSourceList = {} --clean 'global' source list
   local tList = control.listAvailableSources()
   for nI=1, #tList do
      if (tList[nI]['name'] ~= 'VGA1') then
         local tSource = {}
         tSource['nId'] = tList[nI]['id']
         if (tList[nI]['type'] == 'Tuner') then
            tSource['sName'] = 'TV'
         else
            tSource['sName'] = tList[nI]['name']
         end
         tSource['bConnected'] = tList[nI]['connected']
         tSource['sScreenName'] = tList[nI]['screenName']
         tSource['sType'] = tList[nI]['type']
         table.insert(tSourceList, tSource)
      end
   end
   table.sort(tSourceList, function(tA,tB) return tA.nId < tB.nId end)
end

--- Get the size of the sources list
-- @return number of sources
function tInputApp.getSourceListSize()
   --@TODO verify if needed
   return #tSourceList
end

------------------------------------------------------------------------
--handlers implemented
local tHandlers = {}
------------------------------------------------------------------------
--handlers to GUI

------------------------------------------------------------------------
return tInputApp
